﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>apply函数实现</title>
</head>
<body>
    <script type="text/javascript">
        Function.prototype.meApply = function () {
            var bindThis = null
            var this_ = this
            if (arguments.length === 0) {
                bindThis = Object(null)
            } else {
                bindThis = Object(arguments[ 0 ])
            }

            var args = []
            if (arguments.length > 1) {
                args = Array.from(arguments).slice(1, arguments.length)
            }
            bindThis.fun = this
            // 如果这里不允许使用高级语法，可以使用eval拼接参数的方式进行调用，就是效率比较低和代码安全性低
            var res = bindThis.fun(...args)
            delete bindThis.fun
            return res
        }
        function doSomething (name, age) {
            console.log(name, age)
            console.log(this.hh)
        }
        doSomething.meApply({
            hh: 'hh'
        }, 1, 2)
    </script>
</body>
</html>
